<template>
  <el-container class="home-container">
    <!--     头部-->
    <el-header>
      <div class="logo-title">
        <img src="~@/assets/logo.png" alt="">
        <span>瓷器后台管理系统</span>
        <i :class="{'el-icon-s-fold':true,isActive:true}" @click="isAsideShow"></i>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <!--    页面主体-->
    <el-container>
      <!--      侧边栏-->
      <el-aside :width="!isActive?'64px':'200px'" :class="{'initWidth':this.isActive, 'isFlat' :!this.isActive }">
        <div class="toggle-button mobile-hidden" @click="isActive=!isActive">|||</div>
        <el-menu
            :collapse="!isActive"
            background-color="#363D40"
            text-color="#fff"
            :collapse-transition="false"
            router
            unique-opened
            active-text-color="#4F97EF"
            :default-active="activePath"
        >
          <!--          一级菜单-->
          <el-submenu :index="item.id+''" v-for="item in menuList" :key="item.id">
            <!--            一级菜单模板区域-->
            <template slot="title">
              <i :class="iconsObj[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>
            <!--            二级菜单-->
            <el-menu-item :index="'/'+subItem.path" v-for="subItem in item.children" :key="subItem.id"
                          @click="saveNavState('/'+subItem.path)"
            >
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{subItem.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!--      右侧内容主体-->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: 'Home',
  data() {
    return {
      isActive: true,
      menuList: [],
      iconsObj: {},
      activePath: ''
    }
  },
  created() {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    logout() {
      window.sessionStorage.removeItem('token')
      this.$router.push('/login')
    },
    //获取所有的菜单-页面一加载
    async getMenuList() {
      const { data: result } = await this.$http.get('/menus')
      if (result.meta.status !== 200) return this.$message.error(result.meta.msg)
      this.menuList = result.data
      //组织图标数据
      let arr = ['iconfont icon-user', 'iconfont icon-tijikongjian', 'iconfont icon-shangpin', 'iconfont icon-danju', 'iconfont icon-baobiao']
      result.data.map((item, index) => {
        this.iconsObj[item.id + ''] = arr[index]
      })
    },
    //子菜单不高亮,我们解决方案先存起来,刷新后从里面拿赋值给default-active
    saveNavState(activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
      //todo..当点击后,能否让aside自动隐藏
      this.isAsideShow()
    },
    isAsideShow() {
      this.isActive = !this.isActive
    }
  }
}
</script>

<style scoped>
  @import "~@/assets/css/layout.css";
</style>